package android.bignerdranch.shiyan4;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnBindService;
    private Button btnUnbindService;
    private Button btnGetStatus;
    private TextView textView;
    AIDLService aidlService;
    MyService.MyServiceBinder serviceBinder;
    boolean isServiceBind=false;
    ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //返回Service的 Binder对象
            System.out.println("--Service Connected--");
            serviceBinder = (MyService.MyServiceBinder)service;
            isServiceBind = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnBindService =  (Button)findViewById(R.id.btn_main_activity_bind_service);
        btnUnbindService =  (Button)findViewById(R.id.btn_main_activity_unbind_service);
        btnGetStatus =  (Button)findViewById(R.id.btn_main_activity_get_status);
        textView = (TextView)findViewById(R.id.tv);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
        btnGetStatus.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        intent.setClass(MainActivity.this,MyService.class);
        final  int bind_service = R.id.btn_main_activity_bind_service;
        final  int get_status = R.id.btn_main_activity_get_status;
        final  int unbind_service = R.id.btn_main_activity_unbind_service;
        switch (v.getId()){
            case bind_service:{
                if(!isServiceBind){
                    bindService(intent,conn, Service.BIND_AUTO_CREATE);
                    textView.setText("已绑定服务");
                    System.out.println("--bind_service--");
                }
                //如果service尚未绑定就绑定，如果已经绑定则忽略
                break;}
            case get_status :{
                //如果service已经绑定，获取 service 的count计数并显示在截面上。
                if(isServiceBind){
                    textView.setText("当前服务状态:"+serviceBinder.getCount());
                    System.out.println("--get_state--");
                }
                break;}
            case unbind_service:{
                //如果 service已经绑定，则可以解绑，否则忽略
                if(isServiceBind){
                    unbindService(conn);
                    textView.setText("已解绑服务");
                    System.out.println("--unbind_service--");
                    isServiceBind = false;
                }
                break;}
        }
    }
}